package com.code.hfd.observer.self;

import com.code.hfd.observer.DisplayElement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author xi.yang
 * @create 2019-05-04 20:29
 **/
public class StatisticsDisplay implements Observer, DisplayElement {
    private List<Float> temperatures;
    private Subject weatherData;

    public StatisticsDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        this.temperatures = new ArrayList<>();
        weatherData.registerObserver(this);
    }

    /**
     * 暂时不打印出平均数
     */
    @Override
    public void display() {
        System.out.println("Min temperature : " + temperatures.get(0) + "F degress and Max temperature :" + temperatures.get(temperatures.size()-1) + "F degress");
    }

    @Override
    public void update(float temp, float humidity, float pressure) {
        this.temperatures.add(temp);
        Collections.sort(temperatures);
        display();
    }
}
